package com.w3c.pragmatic.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.w3c.pragmatic.domain.req.ProductPageReq;
import com.w3c.pragmatic.domain.res.PageRes;
import com.w3c.pragmatic.domain.res.ProductInfoRes;
import com.w3c.pragmatic.entity.ProductInfo;
import com.w3c.pragmatic.mapper.ProductInfoMapper;
import com.w3c.pragmatic.service.IProductInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 产品信息表 服务实现类
 *
 * @author wangdong
 * @since 2025-02-20
 */
@Service
public class ProductInfoServiceImpl extends ServiceImpl<ProductInfoMapper, ProductInfo>
    implements IProductInfoService {

  @Resource private ProductInfoMapper productInfoMapper;

  @Override
  public PageRes<ProductInfoRes> findProductPageLambda(ProductPageReq req) {
    // 设置分页参数
    PageHelper.startPage(req.getCurrent(), req.getSize());
    // 设置查询语句
    LambdaQueryWrapper<ProductInfo> lambdaQuery = new QueryWrapper<ProductInfo>().lambda();
    if (StrUtil.isNotEmpty(req.getProductType()))
      lambdaQuery.eq(ProductInfo::getProductType, req.getProductType());
    if (StrUtil.isNotEmpty(req.getProductName()))
      lambdaQuery.eq(ProductInfo::getProductName, req.getProductName());
    List<ProductInfo> productInfoList = baseMapper.selectList(lambdaQuery);
    // 将查询结果包装成PageInfo对象 返回（这里将pageHelper的PageInfo简化成我们自己的）
    return PageRes.toPageRes(new PageInfo<>(productInfoList), ProductInfoRes::genToRes);
  }

  @Override
  public PageRes<ProductInfoRes> findProductPageMapper(ProductPageReq req) {
    // 设置分页参数
    PageHelper.startPage(req.getCurrent(), req.getSize());
    // 列表查询
    List<ProductInfoRes> productInfoList = productInfoMapper.findProductPageMapper(req);
    // 包装分页参数并返回
    return PageRes.toPageRes(new PageInfo<>(productInfoList));
  }

  @Override
  public List<ProductInfo> findList(String productType) {
    return baseMapper.selectList(
        new QueryWrapper<ProductInfo>().lambda().eq(ProductInfo::getProductType, productType));
  }

  @Override
  public ProductInfo getByProductCode(String productCode) {
    return baseMapper.selectOne(
        new QueryWrapper<ProductInfo>().lambda().eq(ProductInfo::getProductCode, productCode));
  }
}
